<template>
  <div class="app-container">
    <el-card shadow="hover">
      <div style="margin-bottom: 10px;">
        <i class="el-icon-search"></i>
        <span> 筛选搜索</span>
        <el-button
          type="cyan"
          icon="el-icon-search"
          size="mini"
          style="margin-left: 20px;"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </div>
      <el-form
        :model="queryParams"
        ref="queryForm"
        :inline="true"
        v-show="showSearch"
        label-width="120px"
      >
        <el-form-item label="用户账号" prop="userName">
          <el-input
            v-model="queryParams.userName"
            placeholder="请输入用户账号"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
            @clear="handleQuery"
          />
        </el-form-item>
        <el-form-item label="联系电话" prop="phone">
          <el-input
            v-model="queryParams.phone"
            placeholder="请输入联系电话"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
            @clear="handleQuery"
          />
        </el-form-item>
        <!-- <el-form-item label="客户意向等级" prop="grade">
          <el-select
            v-model="queryParams.grade"
            placeholder="请选择客户意向等级"
            @change="handleQuery"
            clearable
          >
            <el-option
              v-for="item in gradeOptions"
              :key="item.id"
              :label="item.label"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item> -->
      </el-form>
    </el-card>
    <el-row :gutter="20" style="margin-top: 20px;">
      <el-col :span="24" :xs="24">
        <el-row :gutter="10" class="mb8">
          <!-- <el-col :span="1.5">
            <el-button
              type="primary"
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd"
              >新增</el-button
            >
          </el-col> -->
          <el-col :span="1.5">
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              :disabled="multiple"
              @click="handleDelete"
              >批量删除</el-button
            >
          </el-col>
        </el-row>

        <el-table
          v-loading="loading"
          :data="kehuList"
          border
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column
            label="用户账号"
            align="center"
            min-width="200"
            prop="userName"
            :show-overflow-tooltip="true"
          />
          <el-table-column label="用户头像" align="center" width="150">
            <template slot-scope="scope">
              <img class="study_pic" :src="scope.row.headPortrait" alt="" />
            </template>
          </el-table-column>
          <el-table-column
            label="联系电话"
            align="center"
            prop="phone"
            min-width="150"
          />
          <el-table-column
            label="创建时间"
            align="center"
            prop="createTime"
            min-width="160"
          >
            <template slot-scope="scope">
              <span>{{ formatT(scope.row.createTime) }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="操作"
            min-width="150"
            align="center"
            fixed="right"
          >
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="resetPassword(scope.row)"
                >重置密码</el-button
              >
              <el-button
                v-if="scope.row.userId !== 1"
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total > 0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-col>
    </el-row>

    <!-- 添加或修改 客户信息 对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="60%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="公司名称" prop="companyName">
              <el-input
                v-model="form.companyName"
                placeholder="请输入公司名称"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="公司联系人" prop="linkman">
              <el-input v-model="form.linkman" placeholder="请输入公司联系人" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="公司介绍" prop="desc">
              <el-input
                type="textarea"
                autosize
                placeholder="请输入客户公司介绍"
                v-model="form.desc"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="联系电话" prop="phone">
              <el-input
                v-model="form.phone"
                placeholder="请输入客户联系电话"
                maxlength="11"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="公司地址">
              <el-input
                v-model="form.address"
                placeholder="请输入客户公司地址"
                maxlength="50"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="意向等级" prop="grade">
              <el-select v-model="form.grade" placeholder="请选择客户意向等级">
                <el-option
                  v-for="item in gradeOptions"
                  :key="item.id"
                  :label="item.label"
                  :value="item.id"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="状态" prop="status">
              <el-select v-model="form.status">
                <el-option
                  v-for="item in statusOptions"
                  :key="item.id"
                  :label="item.label"
                  :value="item.id"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="20">
            <el-form-item label="开户行">
              <el-input
                v-model="form.blank"
                placeholder="请输入客户公司开户行"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="20">
            <el-form-item label="公司税号">
              <el-input
                v-model="form.dutyParagraph"
                placeholder="请输入客户公司税号"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  queryKehu,
  addKehu,
  updateKehu,
  delKehu,
  resetPwd
} from "@/api/member";

export default {
  name: "zixun_kehu",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 客户数据
      kehuList: null,
      // 弹出层标题
      title: "",

      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {},
      gradeOptions: [
        {
          id: "1",
          label: "A级"
        },
        {
          id: "2",
          label: "B级"
        },
        {
          id: "3",
          label: "C级"
        },
        {
          id: "4",
          label: "D级"
        },
        {
          id: "5",
          label: "E级"
        }
      ],
      statusOptions: [
        {
          id: "1",
          label: "已合作"
        },
        {
          id: "2",
          label: "待合作"
        }
      ],

      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        companyName: undefined,
        phone: undefined,
        status: undefined,
        grade: undefined
      },
      // 表单校验
      rules: {
        companyName: [
          { required: true, message: "客户公司名称不能为空", trigger: "blur" }
        ],
        linkman: [
          {
            required: true,
            message: "客户公司联系人姓名不能为空",
            trigger: "blur"
          }
        ],
        phone: [
          {
            required: true,
            message: "客户公司联系电话不能为空",
            trigger: "blur"
          }
        ],
        grade: [
          {
            required: true,
            message: "客户公司意向等级不能为空",
            trigger: "change"
          }
        ],
        status: [
          { required: true, message: "客户公司状态不能为空", trigger: "change" }
        ]
      }
    };
  },
  watch: {},
  created() {
    this.getList();
  },
  methods: {
    resetPassword(row) {
      this.$prompt('请输入"' + row.userName + '"的新密码', "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消"
      })
        .then(({ value }) => {
          resetPwd(value, row.id).then(response => {
            this.msgSuccess("修改成功，新密码是：" + value);
          });
        })
        .catch(() => {});
    },
    /** 查询用户列表 */
    getList() {
      this.loading = true;
      queryKehu(this.queryParams).then(res => {
        console.log(res.rows,'用户列表');
        this.kehuList = res.rows;
        this.total = res.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.page = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      (this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        companyName: undefined,
        phone: undefined,
        status: undefined,
        grade: undefined
      }),
        this.handleQuery();
    },

    // 删除
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    handleDelete(row) {
      const delIds = row.id || this.ids;
      this.$confirm("是否确认删除选中客户数据?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(function() {
          return delKehu(delIds);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        });
    },

    // 新增、修改按钮
    handleAdd() {
      this.open = true;
      this.form = {};
      this.title = "添加客户信息";
    },
    handleUpdate(row) {
      this.open = true;
      this.title = "修改客户信息";
      this.form = row;
    },

    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateKehu(this.form).then(res => {
              if (res.code == 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addKehu(this.form).then(res => {
              if (res.code == 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.getList();
    }
  }
};
</script>

<style scoped>
.study_pic {
  width: 60px;
  height: 60px;
  border-radius: 50%;
}
</style>
